/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.userManagement.controllers;

import domain.userManagement.exceptions.*;
import domain.userManagement.models.*;
import services.security.EncryptedPasswordGenerator;

/**
 *
 * @author peterfrem
 */
public class UserManager {
    
    public static void registerUser(User newUser) throws DuplicateUserException{
	
	//get list of all registered users
	UserList users = UserList.getInstance();
	
	//check if user already exists
	if(users.userExists(newUser.getUsername())){
	    
	    //if user exists, throw exception
	    throw new DuplicateUserException();
	    
	}else{
	    
	    //if user doesn't exist, encrypted plain-text password currently stored
	    String encryptedPassword = EncryptedPasswordGenerator.encryptPassword(newUser.getPassword());

	    //set encrypted password to user object
	    newUser.setPassword(encryptedPassword);
	    
	    //add new user to list of registered users
	    users.addUser(newUser);	   
	}//end if
	
    }//end method registerUser()
    
}//end class UserManager
